﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class admin_information1 : System.Web.UI.Page
{
    DataClassesDataContext db1 = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack == true)
        {
            MultiView1.SetActiveView(View1);
        
        var result = (from p in db1.proc_GetBkgxNowYear()
                      select p).ToList();
        if (result.Count() > 0)
        {
            TextBox1.Text = result.First().schoolCode;
            TextBox2.Text = result.First().schoolName;
        }
        Button1.ForeColor = Color.Red;
        string IP = Request.ServerVariables.Get("Remote_Addr").ToString();
        string nowtime = DateTime.Now.ToString();
        DateTime nowtime1 = Convert.ToDateTime(nowtime);
        int nowusername = Convert.ToInt32(Session["username"]);
        var newrizhi = new rizhi
        {
            userName = nowusername,
            time = nowtime1,
            ip = IP,
            caozuo = "进入基础信息管理"
        };
        db1.rizhi.InsertOnSubmit(newrizhi);
        db1.SubmitChanges();
        }
    }
    private void BindData1()
    {
        GridView2.DataSourceID = "";
        var resultSubject = from p in db1.proc_GetSubject()
                            select p;
        GridView2.DataSource = resultSubject;
        GridView2.DataBind();
    }
    private void BindData2()
    {
        GridView3.DataSourceID = "";
        var proResult = (from p in db1.proc_GetProfessionInfo()
                         select p).ToList();
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("proNum"));
        dt.Columns.Add(new DataColumn("proName"));
        dt.Columns.Add(new DataColumn("proOrder"));
        dt.Columns.Add(new DataColumn("xz"));
        dt.Columns.Add(new DataColumn("SubjectOne"));
        dt.Columns.Add(new DataColumn("SubjectTwo"));
        dt.Columns.Add(new DataColumn("SubjectThree"));
        dt.Columns.Add(new DataColumn("SubjectFour"));

        foreach (proc_GetProfessionInfoResult item in proResult)
        {
            try
            {
                DataRow dr = dt.NewRow();
                var proSubresult = (from p in db1.proc_GetProfessionSubject(item.proNum)
                                    select p).ToList();
                dr["proNum"] = item.proNum;
                dr["proName"] = item.proName;
                dr["proOrder"] = item.proOrder;
                dr["xz"] = item.xz;
                dr["SubjectOne"] = proSubresult[0].subNum;
                dr["SubjectTwo"] = proSubresult[1].subNum;
                dr["SubjectThree"] = proSubresult[2].subNum;
                dr["SubjectFour"] = proSubresult[3].subNum;     
                dt.Rows.Add(dr);
            }
            catch (Exception ex)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('获取失败，请重试！)</script>");
            }

        }

        GridView3.DataSource = dt;
        GridView3.DataBind();
    }
    private void BindData3()
    {
        GridView5.DataSourceID = "";
        var resultOpenDate = from p in db1.proc_GetOpenDate()
                             select p;
        GridView5.DataSource = resultOpenDate;
        GridView5.DataBind(); 
    }
    private void BindData4()
    {
        GridView6.DataSourceID = "";
        var result = from p in db1.schoolinformation
                     select p;
        GridView6.DataSource = result;
        GridView6.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View1);
        var result = (from p in db1.proc_GetBkgxNowYear()
                      select p).ToList();
        if (result.Count() > 0)
        {  
            TextBox1.Text = result.First().schoolCode;
            TextBox2.Text = result.First().schoolName;
        }
        Button1.ForeColor = Color.Red;
        Button2.ForeColor = Color.White;
        Button3.ForeColor = Color.White;
        Button4.ForeColor = Color.White;
        Button5.ForeColor = Color.White;
        Button6.ForeColor = Color.White;
        Button7.ForeColor = Color.White;
        

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View2);
        var result = (from p in db1.proc_GetBkgxNowYear()
                      select p).ToList();
        if (result.Count() > 0)
        {
           TextBox4.Text = result.First().dYear;
        }
        Button1.ForeColor = Color.White;
        Button2.ForeColor = Color.Red;
        Button3.ForeColor = Color.White;
        Button4.ForeColor = Color.White;
        Button5.ForeColor = Color.White;
        Button6.ForeColor = Color.White;
        Button7.ForeColor = Color.White;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        /*MultiView1.SetActiveView(View3);
        BindData3();
        Button1.ForeColor = Color.White;
        Button2.ForeColor = Color.White;
        Button3.ForeColor = Color.Red;
        Button4.ForeColor = Color.White;
        Button5.ForeColor = Color.White;
        Button6.ForeColor = Color.White;
        Button7.ForeColor = Color.White;*/
        Response.Redirect("opendata.aspx");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View4);
        var resultTestDate = from p in db1.proc_GetTestDate()
                             select p;
        var TestDate = resultTestDate.ToList();
        TextBox5.Text = TestDate[0].testTime;
        TextBox6.Text = TestDate[1].testTime;
        TextBox7.Text = TestDate[2].testTime;
        TextBox8.Text = TestDate[3].testTime;
        Button1.ForeColor = Color.White;
        Button2.ForeColor = Color.White;
        Button3.ForeColor = Color.White;
        Button4.ForeColor = Color.Red;
        Button5.ForeColor = Color.White;
        Button6.ForeColor = Color.White;
        Button7.ForeColor = Color.White;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View5);
        BindData1();
        Button1.ForeColor = Color.White;
        Button2.ForeColor = Color.White;
        Button3.ForeColor = Color.White;
        Button4.ForeColor = Color.White;
        Button5.ForeColor = Color.Red;
        Button6.ForeColor = Color.White;
        Button7.ForeColor = Color.White;
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View6);
        BindData2();
        Button1.ForeColor = Color.White;
        Button2.ForeColor = Color.White;
        Button3.ForeColor = Color.White;
        Button4.ForeColor = Color.White;
        Button5.ForeColor = Color.White;
        Button6.ForeColor = Color.Red;
        Button7.ForeColor = Color.White;
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View7);
        BindData4();
        Panel1.Visible = false;
        Button1.ForeColor = Color.White;
        Button2.ForeColor = Color.White;
        Button3.ForeColor = Color.White;
        Button4.ForeColor = Color.White;
        Button5.ForeColor = Color.White;
        Button6.ForeColor = Color.White;
        Button7.ForeColor = Color.Red;
    }
    protected void Button10_Click1(object sender, EventArgs e)
    {
        try
        {
            db1.proc_UpdateTestDate(1, TextBox5.Text);
            db1.proc_UpdateTestDate(2, TextBox6.Text);
            db1.proc_UpdateTestDate(3, TextBox7.Text);
            db1.proc_UpdateTestDate(4, TextBox8.Text);
            Response.Write("<script>alert('修改成功！')</script>");
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('修改出错！')</script>");
        }
        MultiView1.SetActiveView(View4);
    }
    protected void Button18_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView5.Rows.Count - 1; i++)
        {
            DateTime? startDate = null;
            DateTime? endDate = null;
            string openId = GridView5.DataKeys[i].Value.ToString();
            int openId1 = Convert.ToInt32(openId);
            if ( ((TextBox)GridView5.Rows[i].FindControl("TextBox15")).Text.Trim() != "");
            startDate = Convert.ToDateTime(((TextBox)GridView5.Rows[i].FindControl("TextBox15")).Text.Trim());
            if (((TextBox)GridView5.Rows[i].FindControl("TextBox16")).Text.Trim() != "") ;
            endDate = Convert.ToDateTime(((TextBox)GridView5.Rows[i].FindControl("TextBox16")).Text.Trim());
            db1.proc_UpdateOpenDate(openId1, startDate, endDate);
        }
        Response.Write("<script>alert('保存成功！')</script>");
    }
    protected void Button8_Click1(object sender, EventArgs e)
    {
        if ((db1.proc_AddBkgxNowYear(1, TextBox1.Text.ToString().Trim(), TextBox2.Text.ToString(),"", 0)) == -1)
        {
            Response.Write("<script>alert('保存失败！')</script>");
        }
        else
        {
            Response.Write("<script>alert('保存成功！')</script>");
        }
        MultiView1.SetActiveView(View1);
    }
    protected void Button9_Click1(object sender, EventArgs e)
    {
        if ((db1.proc_AddBkgxNowYear(1,"","", TextBox4.Text.ToString(), 1)) == -1)
        {
            Response.Write("<script>alert('保存失败！')</script>");
        }
        else
        {
            db1.SubmitChanges();
            Response.Write("<script>alert('保存成功！')</script>");
        }
        MultiView1.SetActiveView(View2);
    }
    protected void Button11_Click(object sender, EventArgs e)
    {
        GridView2.DataSourceID = "";
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("subNum"));
        dt.Columns.Add(new DataColumn("subName"));
        foreach (GridViewRow item in GridView2.Rows)
        {
            try
            {
                DataRow dr = dt.NewRow();
                dr["subNum"] = ((TextBox)item.FindControl("TextBox9")).Text;
                dr["subName"] = ((TextBox)item.FindControl("TextBox10")).Text;
                dt.Rows.Add(dr);
            }
            catch (Exception ex)
            {

            }
        }
        DataRow drA = dt.NewRow();
        drA["subNum"] = "";
        drA["subName"] = "";
        dt.Rows.Add(drA);
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
    protected void Button12_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow Row in GridView2.Rows)
        {
            string SubNum = ((TextBox)Row.FindControl("TextBox9")).Text;
            string SubName = ((TextBox)Row.FindControl("TextBox10")).Text;
            if (SubNum != "" && SubName != "")
                db1.proc_UpdateSubject(SubNum, SubName);
        }
        Response.Write("<script>alert('信息已保存！')</script>");
        //ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('信息已保存！')</script>");
        MultiView1.SetActiveView(View5);
        BindData1();
    }
    protected void Button13_Click(object sender, EventArgs e)
    {
        GridView3.DataSourceID = "";
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("proNum"));
        dt.Columns.Add(new DataColumn("proName"));
        dt.Columns.Add(new DataColumn("proOrder"));
        dt.Columns.Add(new DataColumn("xz"));
        dt.Columns.Add(new DataColumn("SubjectOne"));
        dt.Columns.Add(new DataColumn("SubjectTwo"));
        dt.Columns.Add(new DataColumn("SubjectThree"));
        dt.Columns.Add(new DataColumn("SubjectFour"));

        foreach (GridViewRow Row in GridView3.Rows)
        {
                DataRow dr = dt.NewRow();
                dr["proNum"] = ((TextBox)Row.FindControl("TextBox11")).Text;
                dr["proName"] = ((TextBox)Row.FindControl("TextBox12")).Text;
                dr["proOrder"] = ((TextBox)Row.FindControl("TextBox13")).Text;
                dr["xz"] = ((TextBox)Row.FindControl("TextBox14")).Text;
                dr["SubjectOne"] = ((DropDownList)Row.FindControl("DropDownList1")).SelectedValue;
                dr["SubjectTwo"] = ((DropDownList)Row.FindControl("DropDownList2")).SelectedValue;
                dr["SubjectThree"] = ((DropDownList)Row.FindControl("DropDownList3")).SelectedValue;
                dr["SubjectFour"] = ((DropDownList)Row.FindControl("DropDownList4")).SelectedValue;
                dt.Rows.Add(dr);
        }
        DataRow drA = dt.NewRow();
        drA["proNum"] = "";
        drA["proName"] = "";
        drA["proOrder"] = "";
        drA["xz"] = "2";
        drA["SubjectOne"] = "";
        drA["SubjectTwo"] = "";
        drA["SubjectThree"] = "";
        drA["SubjectFour"] = "";
        dt.Rows.Add(drA);
        GridView3.DataSource = dt;
        GridView3.DataBind();
    }
    protected void Button14_Click(object sender, EventArgs e)
    {
        try
        {
            /*db.proc_DeleteProfessionInfo();
           var resc = from p in db.TestSubject
                         select p;
            db.TestSubject.DeleteAllOnSubmit(resc);*/
            foreach (GridViewRow item in GridView3.Rows)
            {
                int? proOrder = null;
                int? xz = null;
                string proNum = ((TextBox)item.FindControl("TextBox11")).Text;
                string proName = ((TextBox)item.FindControl("TextBox12")).Text;
                string Oder = ((TextBox)item.FindControl("TextBox13")).Text;
                string Xz = ((TextBox)item.FindControl("TextBox14")).Text;
                if (Oder != "")
                    proOrder = Convert.ToInt32(((TextBox)item.FindControl("TextBox13")).Text);
                if (Xz != "")
                    xz = Convert.ToInt32(((TextBox)item.FindControl("TextBox14")).Text);
                string SubjectOne = ((DropDownList)item.FindControl("DropDownList1")).SelectedValue;
                string SubjectTwo = ((DropDownList)item.FindControl("DropDownList2")).SelectedValue;
                string SubjectThree = ((DropDownList)item.FindControl("DropDownList3")).SelectedValue;
                string SubjectFour = ((DropDownList)item.FindControl("DropDownList4")).SelectedValue;
                if (proNum != "" && proName != "")
                {
                    db1.proc_UpdateProfessionSubject(proNum, SubjectOne, 1);
                    db1.proc_UpdateProfessionSubject(proNum, SubjectTwo, 2);
                    db1.proc_UpdateProfessionSubject(proNum, SubjectThree, 3);
                    db1.proc_UpdateProfessionSubject(proNum, SubjectFour, 4);
                    db1.proc_UpdateProfessionInfo(proNum, proName, proOrder, xz);
                }
            }
        }
        catch (Exception ex)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('保存失败,请重试！')</script>");
            return;
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "success", "<script>alert('保存成功')</script>");
        BindData2();
    }
    protected void Button15_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        /*GridView6.DataSourceID = "";
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("stuname"));
        dt.Columns.Add(new DataColumn("stuno"));
        foreach (GridViewRow Row in GridView6.Rows)
        {
                DataRow dr = dt.NewRow();
                dr["stuname"] = ((TextBox)Row.FindControl("TextBox17")).Text;
                dt.Rows.Add(dr);
        }
        DataRow drA = dt.NewRow();
        drA["stuno"] = "";
        drA["stuname"] = "";  
        dt.Rows.Add(drA);
        GridView6.DataSource = dt;
        GridView6.DataBind();*/

    }
    protected void Button16_Click(object sender, EventArgs e)
    {
        int? stuno1 = null;
        foreach (GridViewRow item in GridView6.Rows)
        {
            string stuno=((HiddenField)item.FindControl("HiddenField1")).Value;
            string stuname = ((TextBox)item.FindControl("TextBox17")).Text;
                stuno1 = Convert.ToInt32(stuno);
                if (stuname != "")
                {
                    db1.proc_updateschool(stuno1, stuname);
                }
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('修改成功！')</script>");
           
        }
        /*for (int i = 0; i <= GridView6.Rows.Count - 1; i++)
        {
            string stuname = ((TextBox)GridView6.Rows[i].FindControl("TextBox17")).Text;
            string stuno = GridView6.DataKeys[i].Value.ToString();
            if (stuno != "")
            {
                stuno1 = Convert.ToInt32(stuno);
            }
            //Response.Write("<script>alert(stuno1)</script>");
            if (stuname != "")
            {
                db1.proc_updateschoolinformation(stuno1, stuname);
            }

        }*/
        BindData4();
    }
    protected void Button17_Click(object sender, EventArgs e)
    {
        int? stuno1 = null;
        for (int i = 0; i <= GridView6.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.CheckBox checkbox1 = (System.Web.UI.WebControls.CheckBox)GridView6.Rows[i].FindControl("CheckBox1");
            if (checkbox1.Checked == true)
            {
                string stuno =  GridView6.DataKeys[i].Value.ToString();
                if (stuno != "")
                {
                    stuno1 = Convert.ToInt32(stuno);
                }
                db1.delete_schoolinformation(stuno1); 
            }
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('删除成功！')</script>");
        BindData4();
    }
    protected void Button19_Click(object sender, EventArgs e)
    {
        db1.add_school(TextBox18.Text);
        Panel1.Visible = false;
        ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('添加成功！')</script>");
        BindData4();
    }
    protected void GridView6_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 得到该控件
        GridView theGrid = sender as GridView;
        int newPageIndex = 0;
        if (e.NewPageIndex == -3)
        {
            //点击了Go按钮
            TextBox txtNewPageIndex = null;

            //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
            GridViewRow pagerRow = theGrid.BottomPagerRow;

            if (pagerRow != null)
            {
                //得到text控件
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
            }
            if (txtNewPageIndex != null)
            {
                //得到索引
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
            }
        }
        else
        {
            //点击了其他的按钮
            newPageIndex = e.NewPageIndex;
        }
        //防止新索引溢出
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

        //得到新的值
        theGrid.PageIndex = newPageIndex;
        BindData4();
    }
}